package api.ws.server.rocket.receive;

import basic.common.Cst;
import basic.common.bean.log.SysLog;
import basic.common.rocket.MQTask;
import basic.common.rocket.Mq;
import basic.common.rocket.MqModel;
import basic.common.ws.Message;
import basic.common.ws.MsgUser;
import basic.common.ws.UserContainer;
import basic.common.ws.WsCst;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @创建人 xiaojiancheng
 * @时间 2020/5/26
 * @描述 接收日志系统过来的消息 反馈给前台应用
 */
@Mq(topic = WsCst.TopicName.WS_RECEIVE,tag = WsCst.MsgType.RECEIVE_CONSOLE)
public class ReceiveConsoleMsgConsumer implements MQTask {
    @Resource
    private UserContainer userContainer;

    @Override
    public boolean execute(MqModel model, String msgId) {
        SysLog log = model.obj(SysLog.class);

        //  找到就发 找不到就不发  客户端收到的发 会送 ack 信号  后面会过呢据 ack 信号 来判断是否发送
        MsgUser msgUser = userContainer.get(1l);
        if(msgUser == null){
            return true;
        }
        Map<String, Channel> innerMap = msgUser.getInnerMap();

        //  推送消息一定是 Message
        Message rtn = new Message();
        rtn.setType(Cst.MessageType.LOG);
        rtn.setObj(((Map<String, Object>) JSON.toJSON(log)));

        for (Channel value : innerMap.values()) {
            value.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(rtn)));
        }
        return true;
    }
}
